<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>添加消息路由</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

    <link rel="stylesheet" th:href="@{/layui/css/layui.css}"/>
</head>
<body>

<form class="layui-form layui-fluid" action="">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>消息路由</legend>
    </fieldset>
    <div class="layui-form-item">
        <label class="layui-form-label">消息路由名称</label>
        <div class="layui-input-inline">
            <input type="text" name="messageRoutingName" lay-verify="title" autocomplete="off" placeholder="请输入消息路由名称"
                   class="layui-input">
        </div>
    </div>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>请选择设备</legend>
    </fieldset>
    <div class="layui-form-item">
        <label class=" layui-form-label">设备</label>
        <div class=" layui-input-inline">
            <select name="deviceId" lay-filter="aihao">
                <option value=""></option>
                <option th:each="device:${deviceList}" th:value="${device.getDeviceId()}" th:text="${device.getDeviceName()}"></option>
            </select>
        </div>
    </div>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>路由信息</legend>
    </fieldset>
    <div class="layui-form-item">
        <label class=" layui-form-label">路由类型</label>
        <div class=" layui-input-inline">
            <select name="messageRoutingType" lay-filter="aihao">
                <option value=""></option>

                <option value="MOTT" selected="">MQTT</option>
                <option value="ZEROMQ">ZEROMQ</option>
                <option value="REST">REST</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">路由地址</label>
        <div class="layui-input-inline">
            <input type="text" name="messageRoutingAdress" lay-verify="title" autocomplete="off" placeholder="请输入消息路由地址"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">路由端口</label>
        <div class="layui-input-inline">
            <input type="text" name="messageRoutingPort" lay-verify="title" autocomplete="off" placeholder="请输入消息路由端口"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="display: none">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="formVerify">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
<script th:src="@{/layui/layui.js}"></script>
<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form;
        //自定义验证规则
        // form.verify({
        //     title: function (value) {
        //         if (value.length < 5) {
        //             return '标题至少得5个字符啊';
        //         }
        //     }
        //     , pass: [
        //         /^[\S]{6,12}$/
        //         , '密码必须6到12位，且不能出现空格'
        //     ]
        //     , content: function (value) {
        //         layedit.sync(editIndex);
        //     }
        // });
        //监听提交
        form.on('submit(formVerify)', function (obj) {

            var data = JSON.stringify(obj.field);

            var url = "/api/messageRouting";

            var code = 0;
            layui.jquery.ajax({
                type: "post",
                url: url,
                async: false,//同步提交。不设置则默认异步，异步的话，最后执行ajax
                data: data,
                contentType: "application/json",
                // processData: false,
                // cache: false,
                success: function (ev) {
                    code = ev.code;
                }

            });
            if (code == 500) {
                console.log("提交失败");
            }

            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

            parent.layer.close(index);

            // 刷新表格（即点击分页控件的“确定”按钮）
            parent.layui.table.reload("messageRoutingTable");

        });
    })

</script>

</html>